package cn.bistu.cs.music.recyclerAdapter;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.bistu.cs.music.PlayerActivity;
import cn.bistu.cs.music.R;

public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.ViewHolder> {
    private List<Music> musicList;

    public MusicAdapter(List<Music> arrList) {
        this.musicList=arrList;
        for (Music m:musicList) {
            System.out.println(m.getMusicName());
        }
    }

    @NonNull
    @Override
    public MusicAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //创建viewholder
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.music_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        //点击跳转
        holder.musicView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Intent intent = new Intent(v.getContext(), PlayerActivity.class);
                intent.putExtra("myPosition",position);
                Log.e("MusicAdapter定位的歌曲是",musicList.get(position).getMusicName());
                v.getContext().startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MusicAdapter.ViewHolder holder, int position) {
        //点击holder的实例
        Music music = musicList.get(position);
        holder.music_title.setText(music.getMusicName());
        holder.singer_name.setText(music.getSingerName());
    }

    @Override
    public int getItemCount() {
        return musicList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View musicView;
        TextView music_title;
        TextView singer_name;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            musicView = itemView;
            music_title = itemView.findViewById(R.id.music_title);
            singer_name = itemView.findViewById(R.id.singer_name);
        }
    }
}
